﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace AssetManager
{
    public partial class frmMain : Form
    {

        private AssetServiceRef.User user;

        public frmMain(Form Login, AssetServiceRef.User User)
        {
            user = User;
            InitializeComponent();
            FillTree();
            treAssets.ExpandAll();
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            tsmiUser.Text = user.Username.ToString();
        }

        private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            Form frmLogin = new frmLogin();
            frmLogin.StartPosition = FormStartPosition.CenterScreen;
            frmLogin.Show();
        }

        private void userInfoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("ID: " + user.UserID + "\n" +
                            "Username: " + user.Username + "\n" +
                            "Password: " + user.Password);
        }

        private void FillTree()
        {
            AssetServiceRef.IAssetService sr = new AssetServiceRef.AssetServiceClient();
            //AssetServiceRef.AssetServiceClient sr = new AssetServiceRef.AssetServiceClient();

            DataTable dtParent = sr.GetMetaData(user.UserID);
            foreach (DataRow rP in dtParent.Rows)
            {
                TreeNode p = new TreeNode();
                p.Text = rP["Metadata"].ToString();
                p.ImageIndex = 0;
                DataTable dtChild = sr.GetAssets(user.UserID, rP["Metadata"].ToString());
                foreach (DataRow rC in dtChild.Rows)
                {
                    TreeNode c = new TreeNode();
                    c.Text = rC["Name"].ToString();
                    c.Tag = rC["pkAssetID"].ToString();
                    c.ImageIndex = 0;
                    p.Nodes.Add(c);
                }
                treAssets.Nodes.Add(p);
            }
            
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void treAssets_MouseDoubleClick(object sender, TreeViewEventArgs e)
        {
            if (e.Node.Parent != null && treAssets.SelectedNode != null)
            {
                int assetID = Convert.ToInt32(treAssets.SelectedNode.Tag);

                AssetDetails details = new AssetDetails(assetID);
                details.MdiParent = this;
                details.Show();
                details.Dock = DockStyle.Fill;
            }
        }

        //private void treAssets_DoubleClick(object sender, EventArgs e)
        //{
        //    if (treAssets.SelectedNode != null /*&& e.Node.Parent != null*/)
        //    {
        //        MessageBox.Show(treAssets.SelectedNode.Tag.ToString());

        //        int assetID = Convert.ToInt32(treAssets.SelectedNode.Tag);

        //        AssetDetails details = new AssetDetails(assetID);
        //        details.MdiParent = this;
        //        details.Show();
        //        details.Dock = DockStyle.Fill;
        //    }
        //}
    }
}
